<template>
  <div id="addProblemView">
    <h2>创建题目</h2>
    <a-form :model="form" label-align="left" auto-label-width>
      <a-form-item field="title" label="题目名称">
        <a-input v-model="form.title" placeholder="请输入题目名称" />
      </a-form-item>
      <a-form-item field="content" label="题目描述">
        <MDEdit
          style="min-width: 1000px"
          :value="form.content"
          :handle-change="onContentChange"
        />
      </a-form-item>
      <a-form-item field="content" label="题目描述">
        <a-select v-model="form.difficulty" placeholder="请选择题目难度">
          <a-option>简单</a-option>
          <a-option>中等</a-option>
          <a-option>困难</a-option>
        </a-select>
      </a-form-item>
      <a-form-item field="tags" label="题目标签">
        <a-input-tag
          v-model="form.tags"
          placeholder="请设定题目相关标签"
          allow-clear
        />
      </a-form-item>
      <a-form-item field="answer" label="题目答案">
        <MDEdit
          style="min-width: 1000px"
          :value="form.answer"
          :handle-change="onAnswerChange"
        />
      </a-form-item>
      <a-form-item label="判题配置" :content-flex="false" :merge-props="false">
        <a-space direction="vertical" style="min-width: 480px">
          <a-form-item field="judgeConfig.timeLimit" label="时间限制">
            <a-input-number
              v-model="form.judgeConfig.timeLimit"
              :default-value="1000"
              mode="button"
              size="large"
            />
          </a-form-item>
          <a-form-item field="judgeConfig.memoryLimit" label="内存限制">
            <a-input-number
              v-model="form.judgeConfig.memoryLimit"
              :default-value="1000"
              mode="button"
              size="large"
            />
          </a-form-item>
          <a-form-item field="judgeConfig.stackLimit" label="堆栈限制">
            <a-input-number
              v-model="form.judgeConfig.stackLimit"
              :default-value="1000"
              mode="button"
              size="large"
            />
          </a-form-item>
        </a-space>
      </a-form-item>
      <a-form-item
        label="测试用例配置"
        :content-flex="false"
        :merge-props="false"
      >
        <a-form-item
          v-for="(judgeCaseItem, index) of form.judgeCase"
          :key="index"
          no-style
        >
          <a-space
            direction="horizontal"
            align="start"
            style="min-width: 1000px"
          >
            <a-form-item
              :field="form.judgeCase[`${index}`].input"
              :label="`input-${index}`"
              :key="index"
            >
              <a-input
                v-model="judgeCaseItem.input"
                placeholder="请输入输入测试用例"
              />
            </a-form-item>
            <a-form-item
              :field="form.judgeCase[`${index}`].output"
              :label="`output-${index}`"
              :key="index"
            >
              <a-input
                v-model="judgeCaseItem.output"
                placeholder="请输入对应的输出测试用例"
              />
            </a-form-item>
            <a-form-item>
              <a-button status="danger" @click="handleDelete(index)"
                >Delete
              </a-button>
            </a-form-item>
          </a-space>
        </a-form-item>
        <div>
          <a-button status="success" @click="handleAdd">新增用例</a-button>
        </div>
      </a-form-item>
      <a-form-item>
        <a-button
          type="primary"
          shape="round"
          html-type="submit"
          @click="doSubmit"
          >Submit
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import MDEdit from "@/components/MDEdit.vue";
import CodeEditor from "@/components/CodeEditor.vue";
import { ProblemControllerService } from "../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRoute } from "vue-router";

let form = ref({
  title: "",
  content: "",
  difficulty: "",
  tags: [],
  answer: "",
  judgeCase: [
    {
      input: "",
      output: "",
    },
  ],
  judgeConfig: {
    memoryLimit: 1000,
    timeLimit: 1000,
    stackLimit: 1000,
  },
});

const route = useRoute();
//区分update页面
const updatePage = route.path.includes("update");

const loadData = async () => {
  const id = route.query.id;
  if (!id) {
    return;
  }
  const res = await ProblemControllerService.getProblemByIdUsingGet(id as any);
  if (res.code === 0) {
    form.value = res.data as any;
    //json 转 js
    if (!form.value.judgeCase) {
      form.value.judgeCase = [
        {
          input: "",
          output: "",
        },
      ];
    } else {
      form.value.judgeCase = JSON.parse(form.value.judgeCase as any);
    }
    if (!form.value.judgeConfig) {
      form.value.judgeConfig = {
        timeLimit: 1000,
        memoryLimit: 1000,
        stackLimit: 1000,
      };
    } else {
      form.value.judgeConfig = JSON.parse(form.value.judgeConfig as any);
    }
    if (!form.value.tags) {
      form.value.tags = [];
    } else {
      form.value.tags = JSON.parse(form.value.tags as any);
    }
  } else {
    message.error("请求数据失败，" + res.message);
  }
};

onMounted(() => {
  loadData();
});

//增加测试用例
const handleAdd = () => {
  form.value.judgeCase.push({
    input: "",
    output: "",
  });
};
//删除测试用例
const handleDelete = (index: number) => {
  form.value.judgeCase.splice(index, 1);
};

const onContentChange = (v: string) => {
  form.value.content = v;
};

const onAnswerChange = (v: string) => {
  form.value.answer = v;
};

//提交表单
const doSubmit = async () => {
  // console.log(form);
  if (updatePage) {
    const res = await ProblemControllerService.updateProblemUsingPost(
      form.value
    );
    if (res.code === 0) {
      message.success("题目修改成功");
    } else {
      message.error("题目修改失败" + res.message);
    }
  } else {
    const res = await ProblemControllerService.addProblemUsingPost(form.value);
    if (res.code === 0) {
      message.success("题目创建成功");
    } else {
      message.error("题目创建失败" + res.message);
    }
  }
};
</script>

<style scoped></style>
